home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_078 / mandelvroom / savemand.c < prev   
C/C++ Source or Header  |  1992-05-06  |  9KB  |  395 lines

  1. /***************************************************************************
  2.  *
  3.  *                MandelVroom Load/Save raw information
  4.  *
  5.  *                         Kevin L. Clague
  6.  *
  7.  *                        Copyright (C) 1987
  8.  *
  9.  **************************************************************************/
  10.  
  11. #include "mand.h"
  12.  
  13. extern struct NewScreen NewScreen;
  14. extern struct Screen    *screen;
  15. extern struct ViewPort  *vp;
  16.  
  17. extern float StartX,StartY,EndX,EndY;
  18.  
  19. extern SHORT Zoom;
  20.  
  21. extern SHORT CountX,CountY,MaxCount,*CountBase;
  22. extern LONG  NavTop,NavBot,NavLeft,NavRight;
  23.  
  24. extern SHORT NumContours, *ContourBase;
  25. extern UBYTE *ColorBase;
  26.  
  27. extern SHORT SavePalette[], Inited;
  28.  
  29. extern ULONG CalcTime;
  30. extern UBYTE MandType;
  31. extern ULONG BorderType;
  32.  
  33. /*
  34.  * Save all the data needed to restore the system to current state.
  35.  */
  36. SaveCounts(SaveName)
  37.   char *SaveName;
  38. {
  39.   FILE  *SaveFile;
  40.   SHORT *CountPtr,Color;
  41.   ULONG  i,Count,Word = 0;
  42.   USHORT Cur,t;
  43.   ULONG Version = VERSION;
  44.   char   ErrMsg[80];
  45.  
  46.   /*
  47.    * Change the mouse pointer to Sleepy pointer
  48.    */
  49.   SetSleepyPointer();
  50.  
  51.   /*
  52.    * open the desired file for writing
  53.    */
  54.   SaveFile = fopen(SaveName,"w");
  55.  
  56.   if (SaveFile == (FILE *) NULL) {
  57.     sprintf(ErrMsg, "Can't open file %s", SaveName);
  58.     DispErrMsg(ErrMsg, 0);
  59.     return(0);
  60.   }
  61.  
  62.   /*
  63.    * if there is data to save, save it
  64.    */
  65.   if (CountBase) {
  66.  
  67.     /*
  68.      * Put out the file header
  69.      */
  70.     fwrite((char *) "MAND",    4,  1, SaveFile);
  71.  
  72.     /*
  73.      * Put out the revision number
  74.      */
  75.     fwrite((char *) Version,   sizeof(Version), 1, SaveFile);
  76.  
  77.     /*
  78.      * Put out the location on the complex plane
  79.      */
  80.     fwrite((char *) &StartX,   sizeof(float),   1, SaveFile);
  81.     fwrite((char *) &StartY,   sizeof(float),   1, SaveFile);
  82.     fwrite((char *) &EndX,     sizeof(float),   1, SaveFile);
  83.     fwrite((char *) &EndY,     sizeof(float),   1, SaveFile);
  84.  
  85.     /*
  86.      * Put out the Maximum iteration count for a given point
  87.      */
  88.     fwrite((char *) &MaxCount, sizeof(SHORT),    1, SaveFile);
  89.  
  90.     /*
  91.      * Put out the generator type
  92.      */
  93.     fwrite((char *) &MandType, sizeof(MandType), 1, SaveFile);
  94.  
  95.     /*
  96.      * Put out the time it took to generate
  97.      */
  98.     fwrite((char *) &CalcTime, sizeof(CalcTime), 1, SaveFile);
  99.  
  100.     /*
  101.      * Put out the screen's viewmodes
  102.      */
  103.     fwrite((char *) &screen->ViewPort.Modes,
  104.               sizeof(screen->ViewPort.Modes), 1, SaveFile);
  105.  
  106.     /*
  107.      * Put out the number of bit planes
  108.      */
  109.     fwrite((char *) &screen->BitMap.Depth,
  110.               sizeof(screen->BitMap.Depth), 1, SaveFile);
  111.  
  112.     /*
  113.      * Put some window flags
  114.      */
  115.     fwrite((char *) &BorderType, sizeof(BorderType), 1, SaveFile);
  116.  
  117.     /*
  118.      * Save the Color Palette
  119.      */
  120.     for (i = 0; i < 32; i++) {
  121.       Color = GetRGB4(vp->ColorMap, i);
  122.       fwrite((char *) &Color, sizeof(Color), 1, SaveFile);
  123.     }
  124.  
  125.     /*
  126.      * Save the number of contours
  127.      */
  128.     fwrite((char *) &NumContours, sizeof(NumContours), 1, SaveFile);
  129.  
  130.     /*
  131.      * Save the contour's heights
  132.      */
  133.     fwrite((char *) ContourBase, sizeof(SHORT), (long) NumContours, SaveFile);
  134.  
  135.     /*
  136.      * Save the contour's pen numbers (Color is a misnomer)
  137.      */
  138.     fwrite((char *) ColorBase, sizeof(UBYTE), (long) NumContours, SaveFile);
  139.  
  140.     /*
  141.      * Put out the image dimensions
  142.      */
  143.     fwrite((char *) &CountX,   sizeof(SHORT),   1, SaveFile);
  144.     fwrite((char *) &CountY,   sizeof(SHORT),   1, SaveFile);
  145.  
  146.     /* Save count information in pseudo RLL format */
  147.     /* Bits 9-0 are iteration count.               */
  148.     /* Bits 15-10 are how many iteration count of same height in a row */
  149.  
  150.     i = CountX*CountY;
  151.  
  152.     CountPtr = CountBase;
  153.     Cur = *CountPtr;
  154.     Count = 1;
  155.  
  156.     while (--i) {
  157.       CountPtr++;
  158.  
  159.       /*
  160.        * Count up how many in a row have same height
  161.        */
  162.       if (Cur == *CountPtr) {
  163.         Count++;
  164.       } else {
  165.  
  166.         /*
  167.          * Break it up into chunks of 64
  168.          */
  169.         while (Count) {
  170.           if (Count > 63) {
  171.             t = 63 << 10 | Cur;
  172.             Count -= 63;
  173.           } else {
  174.             t = Count << 10 | Cur;
  175.             Count = 0;
  176.           }
  177.           fwrite(&t,sizeof(SHORT),1,SaveFile);
  178.           Word++;
  179.         }
  180.         Count = 1;
  181.         Cur = *CountPtr;
  182.       }
  183.     }
  184.  
  185.     /*
  186.      * Break the last one up into chunks
  187.      */
  188.     while (Count) {
  189.       if (Count > 63) {
  190.         t = 63 << 10 | Cur;
  191.         Count -= 63;
  192.       } else {
  193.         t = Count << 10 | Cur;
  194.         Count = 0;
  195.       }
  196.       fwrite(&t,sizeof(SHORT),1,SaveFile);
  197.       Word++;
  198.     }
  199.  
  200.   } else {
  201.     DispErrMsg("No Picture to save",0);
  202.     fclose(SaveFile);
  203.     return(0);
  204.   }
  205.   fclose(SaveFile);
  206. } /* SaveCounts */
  207.  
  208. /*
  209.  * Load the state of the program from file
  210.  */
  211. LoadCounts(LoadName)
  212.   char *LoadName;
  213. {
  214.   FILE  *LoadFile;
  215.   SHORT *CountPtr;
  216.   LONG   i;
  217.   ULONG  Count,Word = 0;
  218.   USHORT t;
  219.   USHORT ViewModes;
  220.   UBYTE  Depth;
  221.   UBYTE  Header[5];
  222.   ULONG  Version;
  223.   char   ErrMsg[80];
  224.  
  225.   /*
  226.    * Change the mouse pointer to Sleepy pointer
  227.    */
  228.  
  229.   SetSleepyPointer();
  230.  
  231.   LoadFile = fopen(LoadName,"r");
  232.  
  233.   if (CountBase) {
  234.     FreeMem(CountBase,CountX*CountY*sizeof(SHORT));
  235.     CountBase = (SHORT *) NULL;
  236.   }
  237.  
  238.   if (LoadFile == (FILE *) NULL) {
  239.  
  240.     /*
  241.      * can't read a non-existant file
  242.      */
  243.     sprintf(ErrMsg, "File %s not found", LoadName);
  244.     DispErrMsg(ErrMsg, 0);
  245.     return(-1);
  246.   } else {
  247.     /*
  248.      * Read and check the file header
  249.      */
  250.     fread((char *) &Header[0], 4, 1, LoadFile);
  251.     Header[4] = '\0';
  252.  
  253.     if (strcmp(&Header[0], "MAND") != 0) {
  254.  
  255.       /*
  256.        * File of improper format
  257.        */
  258.       sprintf(ErrMsg, "File %s is not a MAND file",LoadName);
  259.       DispErrMsg(ErrMsg, 0);
  260.       fclose(LoadFile);
  261.       return(-1);
  262.  
  263.     } else {
  264.  
  265.       /*
  266.        * Read in the version
  267.        */
  268.       fread((char *) &Version, sizeof(Version), 1, LoadFile);
  269.  
  270.       /*
  271.        * Read in the locationin the complex plane
  272.        */
  273.       fread((char *) &StartX, sizeof(float), 1, LoadFile);
  274.       fread((char *) &StartY, sizeof(float), 1, LoadFile);
  275.       fread((char *) &EndX,   sizeof(float), 1, LoadFile);
  276.       fread((char *) &EndY,   sizeof(float), 1, LoadFile);
  277.  
  278.       /*
  279.        * Read in the maximum iteration count for a given point
  280.        */
  281.       fread((char *) &MaxCount, sizeof(SHORT), 1, LoadFile);
  282.  
  283.       /*
  284.        * Read in the generator type
  285.        */
  286.       fread((char *) &MandType, sizeof(MandType), 1, LoadFile);
  287.  
  288.       /*
  289.        * Read in the calculation time
  290.        */
  291.       fread((char *) &CalcTime, sizeof(CalcTime), 1, LoadFile);
  292.  
  293.       /*
  294.        * Read in the screen's viewmodes
  295.        */
  296.       fread((char *) &ViewModes, sizeof(ViewModes), 1, LoadFile);
  297.  
  298.       /*
  299.        * Read in the number of bit planes
  300.        */
  301.       fread((char *) &Depth, sizeof(Depth), 1, LoadFile);
  302.  
  303.       /*
  304.        * Read in the window flags
  305.        */
  306.       fread((char *) &BorderType, sizeof(BorderType), 1, LoadFile);
  307.  
  308.       /*
  309.        * Read in the color palette information
  310.        */
  311.       fread((char *) &SavePalette[0], sizeof(SHORT), 32, LoadFile);
  312.  
  313.       /*
  314.        * Read in the number of contours
  315.        */
  316.       fread((char *) &NumContours, sizeof(NumContours), 1, LoadFile);
  317.  
  318.       /*
  319.        * Read in the heights
  320.        */
  321.       fread((char *) ContourBase, sizeof(SHORT), (long) NumContours, LoadFile);
  322.  
  323.       /*
  324.        * Read in the pen numbers (Color is a misnomer)
  325.        */
  326.       fread((char *) ColorBase, sizeof(UBYTE), (long) NumContours, LoadFile);
  327.  
  328.       /*
  329.        * Put out the image dimensions
  330.        */
  331.       fread((char *) &CountX,   sizeof(SHORT),  1, LoadFile);
  332.       fread((char *) &CountY,   sizeof(SHORT),  1, LoadFile);
  333.  
  334.       /*
  335.        * try to allocate the space for the picture's iteration counts
  336.        */
  337.       CountBase = (SHORT *) AllocMem(CountX*CountY*sizeof(SHORT),0);
  338.  
  339.       if (CountBase == (SHORT *) NULL) {
  340.         DispErrMsg("Can't load counts. Out of RAM!!",0);
  341.         fclose(LoadFile);
  342.         return(0);
  343.       }
  344.  
  345.       CountPtr = CountBase;
  346.  
  347.       /* Load count information in pseudo RLL format */
  348.       /* Bits 9-0 are iteration count.               */
  349.       /* Bits 15-10 are how many iteration count of same height in a row */
  350.  
  351.       i = CountX*CountY;
  352.  
  353.       while (i > 0) {
  354.         Word++;
  355.         if (fread((char *) &t, sizeof(t), 1, LoadFile) == 0) {
  356.           DispErrMsg("Premature EOF on source file",0);
  357.           fclose(LoadFile);
  358.           return(0);
  359.         }
  360.         Count = t >> 10;
  361.         t = t & 0x3ff;
  362.         for (; Count > 0; Count--) {
  363.           *(CountPtr++) = t;
  364.           i--;
  365.         }
  366.       }
  367.  
  368.       CalculateGaps();
  369.     }
  370.   }
  371.  
  372.   /*
  373.    * Open a new screen
  374.    */
  375.   NewScreen.ViewModes = ViewModes;
  376.   NewScreen.Depth = Depth;
  377.  
  378.   Inited = 0;
  379.   CloseDisp();
  380.   Inited = 1;
  381.   OpenDisp();
  382.  
  383.   /*
  384.    * Let's show it to them
  385.    */
  386.   ReColor();
  387.  
  388.   /*
  389.    * Set No ZOOM
  390.    */
  391.   Zoom = 0;
  392.   fclose(LoadFile);
  393. } /* NewLoadCounts */
  394.  
  395.